package com.xh.bussiness.system.controller;

import com.xh.bussiness.system.bean.request.CourseSystemRequest;
import com.xh.bussiness.system.bean.request.SaveCourseSystemRequest;
import com.xh.bussiness.system.service.ICourseSystemService;
import com.xh.bussiness.system.model.CourseSystem;
import com.xh.core.bean.XhExportHeaderBean;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;

import com.xh.core.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

/**
 * <p>
 * 课程体系 前端控制器
 * </p>
 *
 * @author Pitt
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/admin/courseSystem")
public class CourseSystemController extends BaseController {

    @Autowired
    private ICourseSystemService courseSystemService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, CourseSystemRequest bean){
        page=courseSystemService.queryCourseSystemPage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(CourseSystemRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",courseSystemService.queryCourseSystem(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(CourseSystemRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=courseSystemService.exportAll(request,exportHeaderBeans,bean,"queryCourseSystemPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param courseSystem 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated CourseSystem courseSystem){
        if(courseSystem.getId()==null){
            courseSystemService.insert(courseSystem);
        }else{
            courseSystemService.update(courseSystem);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        courseSystemService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        courseSystemService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 根据学科查询课程体系
     * @param subjectId
     * @param typeCode 配置项类型
     * @return
     */
    @RequestMapping("/queryCourseSystemBySubject")
    public AjaxResult queryCourseSystemBySubject(String subjectId,String[] typeCode) {
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, courseSystemService.queryCourseSystemBySubject(subjectId, typeCode));
    }


    /**
     * 配置课程体系
     * @param courseSystem 课程体系
     * 类型【年级：GRADE；学期：SEMESTER；教材版本：TEACH_BOOK_VERSION；班级类型：COURSE_TYPE】
     * @return
     */
    @PostMapping("/saveOrUpdateCourseSystemBySubjectId")
    @Transactional
    public AjaxResult saveOrUpdateCourseSystemBySubjectId(@RequestBody CourseSystem courseSystem) {
        return new AjaxResult(courseSystemService.saveOrUpdateCourseSystemBySubjectId(courseSystem));
    }

    /**
     * 批量配置课程体系
     * @param request
     * @return
     */
    @PostMapping("/batchSaveOrUpdateCourseSystemBySubjectId")
    @Transactional
    public AjaxResult batchSaveOrUpdateCourseSystemBySubjectId(@RequestBody SaveCourseSystemRequest request) {
        return new AjaxResult(courseSystemService.batchSaveOrUpdateCourseSystemBySubjectId(request));
    }

}

